<template>
  <el-dialog :visible.sync="visible" v-if="visible" :title="title">
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="父级节点" prop="parentId">
        <el-cascader :props="props" v-model="formData.ps" filterable clearable></el-cascader>
      </el-form-item>
      <el-form-item label="节点名称" prop="brandName">
        <el-input v-model="formData.brandName" placeholder="请输入节点名称"/>
      </el-form-item>
      <el-form-item label="节点首字母">
        <el-input v-model="formData.firstLetter" placeholder="请输入节点首字母"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const formData = {
  id: '',
  brandName: '',
  firstLetter: '',
  parentId: 0,
  ps: [0]
}
export default {
  name: 'brandUpdate',
  data () {
    return {
      visible: false,
      title: '添加数据',
      formData: {
        ...formData
      },
      rules: {
        parentId: [{
          required: true,
          message: '请选择父级菜单'
        }],
        brandName: [{
          required: true,
          message: '请输入节点名称'
        }]
      },
      props: {
        value: 'id',
        label: 'brandName',
        lazy: true,
        checkStrictly: true,
        lazyLoad: (node, resolve) => {
          const parentId = node.data?.id ?? 0
          this.$http.get('brand/page', { parentId }).then(data => {
            if (data) {
              const rows = data.rows.map(item => {
                return {
                  ...item,
                  leaf: item.child === 0 || node.level === 1
                }
              })
              if (parentId === 0) {
                rows.unshift({ id: 0, brandName: '顶级节点', leaf: true })
              }
              resolve(rows)
            }
          })
        }
      }
    }
  },
  methods: {
    init (pps, id) {
      this.visible = true
      this.title = id ? '修改数据' : '添加数据'
      this.formData = { ...formData }
      if (pps) {
        this.formData.ps = pps
      }
      if (id) {
        this.$http.get(`brand/info/${id}`).then(data => {
          if (data) {
            this.formData = { ...data }
            if (data.ps) {
              const ps = data.ps.split('-').map(item => {
                return parseInt(item)
              })
              this.formData.ps = ps
            } else {
              this.formData.ps = [0]
            }
          }
        })
      }
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.formData.parentId = this.formData.ps[this.formData.ps.length - 1]
          const submitForm = { ...this.formData }
          if (this.formData.parentId === 0) {
            submitForm.ps = ''
          } else {
            submitForm.ps = submitForm.ps.join('-')
          }
          this.$http.post('brand/update', submitForm).then(() => {
            this.$message.success('操作成功')
            this.visible = false
            this.$emit('closeDialog')
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
